package com.peng.leetcode.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * PostorderTraversal
 * 145. 二叉树的后序遍历
 * https://leetcode.cn/problems/binary-tree-postorder-traversal/
 * <p>
 * Created on 2022/11/30
 *
 * @author lupeng
 */
public class PostorderTraversal {

    List<Integer> ans = new ArrayList<>();

    public List<Integer> postorderTraversal(TreeNode root) {
        if (root == null) {
            return ans;
        }
        dfs(root);
        return ans;
    }

    void dfs(TreeNode n) {
        if (n == null) {
            return;
        }
        dfs(n.left);
        dfs(n.right);
        ans.add(n.val);
    }
}
